package com.supersit.greensproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.supersit.greensproject.R;
import com.supersit.greensproject.resources.Authority;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by cong on 2016/7/13.
 */
public class MenuAdapter extends BaseAdapter {
    private Context context;
    private List<Map<Integer, Map<String, Object>>> menuData;
    private LayoutInflater inflater;

    public MenuAdapter(Context context, List<Map<Integer, Map<String, Object>>> menuData) {
        this.context = context;
        this.menuData = menuData;
        this.inflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        if (null == menuData && menuData.size() <= 0) {
            return 0;
        } else {
            return menuData.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (null == menuData && menuData.size() <= 0) {
            return null;
        } else {
            return menuData.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HolderView holderView;
        if (null == convertView) {
            holderView = new HolderView();
            convertView = inflater.inflate(R.layout.item_menu, null);
            holderView.menuIcon = (ImageView) convertView.findViewById(R.id.iv_menu_icon);
            holderView.menuName = (TextView) convertView.findViewById(R.id.tv_menu_name);
            convertView.setTag(holderView);
        } else {
            holderView = (HolderView) convertView.getTag();
        }
        Map<Integer, Map<String, Object>> item = menuData.get(position);
        for (int key : item.keySet()) {
            Map<String, Object> map = item.get(key);
            holderView.menuIcon.setImageResource((Integer) map.get("itemImage"));
            holderView.menuName.setText(map.get("itemText").toString());
        }
        return convertView;
    }

    class HolderView {
        ImageView menuIcon;
        TextView menuName;
    }
}
